<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp" %>

<html>
<head>
    <title>新建模板</title>
    <meta name="decorator" content="default"/>
    <link href="${ctxStatic}/css/base.css" rel="stylesheet" type="text/css"/>
    <script src="${ctxStatic}/js/jquery-1.9.1.min.js" type="text/javascript"></script>
    <script type="text/javascript" src="${ctxStatic}/ckeditor/ckeditor.js"></script>
    <script type="text/javascript" src="${ctxStatic}/jquery-jbox/2.3/jquery.jBox-2.3.min.js"></script>
    <link href="${ctxStatic}/jquery-jbox/2.3/Skins/Bootstrap/jbox.css" rel="stylesheet" type="text/css"/>
    <script type="text/javascript">
        function submitModel() {
            var name = $("#templateName").val();
            var re = RegExp(/[(\ )(\~)(\·)(\`)(\!)(\@)(\#)(\$)(\%)(\^)(\&)(\*)(\()(\))(\-)(\_)(\+)(\=)  (\[)(\])(\{)(\})(\|)(\\)(\;)(\:)(\')(\")(\,)(\.)(\/)(\<)(\>)(\?)(\)]+/);
            if (re.test(name)) {
                top.$.jBox.alert("模板名不允许包含特殊字符", "提示");
                return;
            }
            var sendM = $("#sendMethod").val();
            if (sendM == "") {
                top.$.jBox.alert("请选择模板发送方式……", "提示");
                return;
            }
            if (name == "") {
                top.$.jBox.alert("请输入模板名称……", "提示");
                return;
            }
            if ($('#sendLevel3').attr('checked') === 'checked') {
                console.log($('#sendTime').val());
                var sendTimeChk = $('#sendTime').val();
                if (sendTimeChk == '') {
                    top.$.jBox.alert("闲时发送必须填写发送时间……", "提示");
                    return;
                }
            }
            var id = $("#id").val();
            if (id == "") {
                $.post("${ctx}/message/check",
                    {"name": name, "method": sendM, "pac4jCsrfToken": $("#pac4jCsrfToken").val()},
                    function (data) {
                        if (data == "1") {
                            $("#searchForm").submit();
                        } else {
                            top.$.jBox.alert("该发送方式已经存在一个相同的模板名称，请重新输入模板名称！", "提示");
                        }
                    });
            } else {
                $("#searchForm").submit();
            }
        }

        function Cclick() {
            $("#id").val("");
            $("#templateName").val("");
            $("#sendMethod").val("").trigger("change");
            $("#templatType").val("").trigger("change");
        }
    </script>
</head>
<body>
<ul class="nav nav-tabs">
    <li><a href="${ctx}/message/listModel">模板管理</a></li>
    <li class="active"><a href="${ctx}/message/editModel">模板新增/修改</a></li>
</ul>
<form:form id="searchForm" modelAttribute="PfModel" action="${ctx}/message/edit" method="post"
           class="breadcrumb form-search">
    <input id="pac4jCsrfToken" name="pac4jCsrfToken" type="hidden"
           value="<%=request.getAttribute("pac4jCsrfToken")%>"/>
    <div>
        <input type="hidden" id="id" name="id"/>
        <input type="hidden" id="status" name="status"/>
        <label>模板名称：</label><input id="templateName" name="templateName" maxlength="20" class="input-medium"/>&nbsp;&nbsp;
        <label>推送方式：</label>
        <select id="sendMethod" name="sendMethod" class="input-medium" onchange="changeType(this.value)">
            <c:forEach items="${fns:getDictList('model_send_type')}" var="item">
                <option value="${item.value}">${item.label}</option>
            </c:forEach>
        </select>&nbsp;&nbsp;
        <label>业务类型：</label>
        <select id="templatType" name="templatType" class="input-medium">
            <c:forEach items="${fns:getDictList('sys_data_msg_busniess_type')}" var="item">
                <option value="${item.value}">${item.label}</option>
            </c:forEach>
        </select>&nbsp;&nbsp;&nbsp;&nbsp;
    </div>
    &nbsp;
    &nbsp;
    &nbsp;
    <div>
        <input id="btnSubmit" class="btn btn-primary" type="button" onclick="submitModel()" value="提交"/>&nbsp;&nbsp;
        <input id="btnDelTeam" class="btn" type="button" value="取&nbsp;&nbsp;消" onclick="Cclick()"/>
    </div>
    <tags:message content="${message}"/>
    &nbsp;
    &nbsp;
    <div>
        <input id="sendLevel1" type="radio" value="1" name="sendLevel" onclick="changeLevelPanel(1);"/>即时消息&nbsp;&nbsp;
        <input id="sendLevel3" type="radio" value="3" name="sendLevel" onclick="changeLevelPanel(3);"/>定时消息&nbsp;&nbsp;
    </div>
    &nbsp;
    <div class="timely" hidden="hidden">
            <%--即时消息--%>
    </div>
    &nbsp;
    <div class="commonly">
            <%--普通消息--%>
    </div>
    &nbsp;
    <div class="whenidle" hidden="hidden">
        发送时间： <input id="sendTime" name="sendTime" type="text" maxlength="50"
                     class="input-large Wdate sendHide" value="${pFModel.sendTime}"
                     onclick="WdatePicker({dateFmt:'HH:mm:ss',isShowClear:false});"/>
    </div>

    &nbsp;
    <div>
        备注：<textarea name="remark" style="width:88%;" rows="4">${pFModel.remark}</textarea>
    </div>
    &nbsp;
</form:form>


<script>
    function changeLevelPanel(level) {
        switch (level) {
            case 1:
                $(".timely").show();
                $(".commonly").hide();
                $(".whenidle").hide();
                $("#sendLevel1").attr("checked", true);
                break;
            case 2:
                $(".timely").hide();
                $(".commonly").show();
                $(".whenidle").hide();
                $("#sendLevel2").attr("checked", true);
                break;
            case 3:
                $(".timely").hide();
                $(".commonly").hide();
                $(".whenidle").show();
                $("#sendLevel3").attr("checked", true);
                break;
        }
    }

    $(function () {
        $(".btnCheckEveryDay").change(function () {
            if ($(this).attr('checked') === 'checked') {
                $(".sendHide").hide();
            } else {
                $(".sendHide").show();
            }
        });
    });
</script>
</body>
</html>